package com.windmill.ui;

import iqq.im.bean.QQAccount;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.windmill.core.WindmillClient;

public class LoginFrame extends BaseFrame {

	// 窗口的用户QQ的输出框
	private JTextField qqField;
	private JPasswordField pwdField;
	private JTextField vcodeField;
	private WindmillClient client;
	private static final long serialVersionUID = -7193462568008136217L;

	public LoginFrame(WindmillClient client) {
		this.client = client;
		init();
	}
	
			
		

	private void init() {
		this.setSize(320, 220);
		this.setLocationRelativeTo(null);// 在窗口中间显示窗口
		this.setContentPane(createContentPane());// 设置窗口中的panel
		this.setUndecorated(true);  //是否要边框
//		this.setAlwaysOnTop(true);  //置顶显示
		ImageIcon backImage = null;
		JLabel backLabel = null;
		try {
			backImage = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("images/login.jpg")));
			backLabel = new JLabel(backImage);
		} catch (IOException e) {

		}
		backLabel.setBounds(0, 0, backImage.getIconWidth(),backImage.getIconHeight());
		((JPanel) this.getContentPane()).setOpaque(false);//如果此组件应该是不透明的，则为 true
		this.getLayeredPane().add(backLabel,new Integer(Integer.MIN_VALUE));
	}




	// 初始化窗口的方法
	public void display() {
//		this.qqField.setText("330060735");
//		this.pwdField.setText("a111111111111111");
		this.qqField.setText("361956936");
		this.pwdField.setText("jja@1045126");
		this.setVisible(true);
	}

	// 创建主panel并初始化
	public JPanel createContentPane() {
		JPanel panel = new JPanel(new BorderLayout());
		JLabel lable = null;
		try {
			lable = new JLabel(new ImageIcon(ImageIO.read(getClass().getResourceAsStream("images/name.gif"))));
		} catch (IOException e) {
			
		}
		panel.add(lable, BorderLayout.NORTH);
		panel.add(createInputPane(), BorderLayout.CENTER);
		panel.add(createBtnPane(), BorderLayout.SOUTH);
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
		// 上 左 下 右
		return panel;
	}

	// 放置3个lable和3个输入框的panel
	public JPanel createInputPane() {
		JPanel panel = new JPanel(new GridLayout(2, 1,0, 3));
		panel.setOpaque(false);
		panel.setBorder(new EmptyBorder(10, 50, 10, 50));
		panel.add(createIdPane());// 添加存放编号输入框的panel
		panel.add(createPwdPane());// 添加存放密码输入况的panel
//		panel.add(createVcodePane());// 添加存放验证码输入况的panel
		return panel;
	}


	// 创建存放编号和他的输入框的panel
	public JPanel createIdPane() {
		JPanel panel = new JPanel(new BorderLayout(6, 0));
		panel.setOpaque(false);
		JLabel label = new JLabel("Ｑ　Ｑ：");
		panel.add(label, BorderLayout.WEST);
		// 创建一个输入框放在CENTER  
		qqField = new JTextField("");
		panel.add(qqField, BorderLayout.CENTER);
		return panel;
	}

	// 创建存放密码输入况的panel
	public JPanel createPwdPane() {
		JPanel panel = new JPanel(new BorderLayout(6, 0));
		panel.setOpaque(false);
		JLabel label = new JLabel("密　码：");
		panel.add(label, BorderLayout.WEST);
		// 创建一个密码输入框放在CENTER
		pwdField = new JPasswordField("");
		// 密码框允许输入发（解决linux不能输入的问题）
		pwdField.enableInputMethods(true);

		panel.add(pwdField, BorderLayout.CENTER);
		return panel;
	}
	public JPanel createVcodePane() {
		JPanel panel = new JPanel(new BorderLayout(6, 0));
		panel.setOpaque(false);
		JLabel label = new JLabel("登录码：");
		panel.add(label, BorderLayout.WEST);
		// 创建一个输入框放在CENTER  
	    vcodeField = new JTextField("BATE版本无需登录码");
		panel.add(vcodeField, BorderLayout.CENTER);
		return panel;
	}

	// 创建放按钮的panel
	public JPanel createBtnPane() {
		JPanel panel = new JPanel(new GridLayout(1, 2,20, 0));
		panel.setOpaque(false);
		panel.setBorder(new EmptyBorder(0, 103, 20, 50));
		JButton login = new JButton("登录");
		login.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 当电机登录按钮 调用控制起ClientContext的登录方法
				client.login(getQQAccount());				
			}
		});
		JButton cancel = new JButton("退出");
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		panel.add(login);
		panel.add(cancel);
		return panel;
	}

	
	/**
	 * @return 登录QQsession
	 */
	public QQAccount getQQAccount(){
		QQAccount account = new QQAccount();
		account.setUsername(qqField.getText());
		account.setPassword(new String(pwdField.getPassword()));		
		return account;
	}
	

	
}
